//
//  SocialSharingHelper.m
//  iFly
//
//  Created by Petko Haydushki on 12/12/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "SocialSharingHelper.h"


@implementation SocialSharingHelper

-(instancetype)init {
    self = [super init];
    if (self) {
        SLComposer=[[SLComposeViewController alloc]init];
    }
    return self;
}

-(void)postToFacebookOrTwitterFromCurrentController:(UIViewController *)controller withPoints:(NSUInteger)points andIsGameWon:(BOOL) gameWon andSocialType:(SocialType)socialType {
    
    SLComposeViewController * socialSheet;
    
    if (socialType== Facebook) {
        socialSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        if (![socialSheet addURL:[NSURL URLWithString:@"http://facebook.com/"]]){
            NSLog(@"Error: Unable to URL");
        }
    }  else {
            socialSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            if (![socialSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
            NSLog(@"Error: Unable to URL");
            }
    }
    // Sets the completion handler
    socialSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without posting
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };
    NSString* initialText;
    if (gameWon) {
        initialText=[NSString stringWithFormat:@"I've just beat down Lokki,and I hit %lu points!!!",(unsigned long)points];
    }
    else {
        initialText=[NSString stringWithFormat: @"I hit %lu points in iFly!!!",(unsigned long)points];
    }
    [socialSheet setInitialText:initialText];
    
    if (![socialSheet addImage:[UIImage imageNamed:@"ironManIcon(180).png"]]) {
        NSLog(@"Error: Unable to add image");
    }
    
    [controller presentViewController:socialSheet animated: YES completion:nil];
}

@end
